package cn.myfirefly.moming.house.service;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.house.dao.SpfTradeContractDao;
import cn.myfirefly.moming.house.dao.SpfTradePerformanceDao;
import cn.myfirefly.moming.house.dao.SpfTradeTrackFormDao;
import cn.myfirefly.moming.house.entity.SpfTradeContract;
import cn.myfirefly.moming.house.entity.SpfTradePerformance;
import cn.myfirefly.moming.house.entity.SpfTradeTrackForm;
import cn.myfirefly.moming.house.model.SpfTradeContractDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class SpfTradeTrackFormService {

	@Autowired
	private SpfTradeTrackFormDao spfTradeTrackFormDao;
	
	@Autowired
	private SpfTradeContractDao spfTradeContractDao;
	
	@Autowired
	private SpfTradePerformanceDao spfTradePerformanceDao;
	
	public PageResultDto<SpfTradeTrackFormDto> getList(SpfTradeTrackFormFilterDto dto) {
		
		return spfTradeTrackFormDao.getList(dto);
	}
	
	public PageResultDto<SpfTradeTrackFormDto> getToBeAuditedList(SpfTradeTrackFormFilterDto dto) {
		
		return spfTradeTrackFormDao.getToBeAuditedList(dto);
	}
	
	public SpfTradeTrackFormDto getSpfTradeTrackForm(Integer id) {
		SpfTradeTrackFormDto spfTradeTrackFormDto = new SpfTradeTrackFormDto();
		SpfTradeTrackForm spfTradeTrackForm = spfTradeTrackFormDao.get(id);
		BeanUtil.copyBeans(spfTradeTrackForm, spfTradeTrackFormDto);
		spfTradeTrackFormDto.setCreateBy(spfTradeTrackForm.getCreateBy());
		spfTradeTrackFormDto.setCreateDate(spfTradeTrackForm.getCreateDate());
		spfTradeTrackFormDto.setModifyBy(spfTradeTrackForm.getModifyBy());
		spfTradeTrackFormDto.setModifyDate(spfTradeTrackForm.getModifyDate());
		SpfTradeContract spfTradeContract = spfTradeContractDao.get(spfTradeTrackForm.getTradeContractId());
		BeanUtil.copyBeans(spfTradeContract, spfTradeTrackFormDto);
		return spfTradeTrackFormDto;
	}
	
	public String del(Integer id) {
		SpfTradeTrackForm spfTradeTrackForm = spfTradeTrackFormDao.get(id);
		if (spfTradeTrackForm == null) {
			return "该信息不存在，请尝试刷新列表后再操作。";
		}
		spfTradeTrackFormDao.del(spfTradeTrackForm);
		return Const.ResultCode.SUCCESS;
	}
	
	public Integer insert(SpfTradeTrackFormDto dto) {
		SpfTradeTrackForm spfTradeTrackForm = BeanUtil.copyBeans(SpfTradeTrackForm.class, dto);
		spfTradeTrackForm.setCreateBy(dto.getCreateBy());
		spfTradeTrackForm.setCreateDate(dto.getCreateDate());
		spfTradeTrackForm.setModifyBy(dto.getModifyBy());
		spfTradeTrackForm.setModifyDate(dto.getModifyDate());
		Integer count = spfTradeTrackFormDao.save(spfTradeTrackForm);
		SpfTradeContract spfTradeContract = spfTradeContractDao.get(spfTradeTrackForm.getTradeContractId());
		spfTradeContract.setTradeContractStatus("2");
		spfTradeContractDao.save(spfTradeContract);
		
		SpfTradePerformance spfTradePerformance = new SpfTradePerformance();
		BigDecimal oldNewAmount = new BigDecimal("3000");
		BigDecimal performanceAmount = spfTradeContract.getReceivableCommission().
				multiply(new BigDecimal("0.9")).subtract(oldNewAmount).setScale(2, RoundingMode.HALF_UP);
		spfTradePerformance.setTradeContractId(spfTradeContract.getId());
		spfTradePerformance.setOldNewAmount(oldNewAmount);
		spfTradePerformance.setPerformanceAmount(performanceAmount);
		spfTradePerformance.setCreateBy(dto.getCreateBy());
		spfTradePerformance.setCreateDate(dto.getCreateDate());
		spfTradePerformance.setModifyBy(dto.getModifyBy());
		spfTradePerformance.setModifyDate(dto.getModifyDate());
		spfTradePerformanceDao.save(spfTradePerformance);
		return count;
	}
	
	public Integer update(SpfTradeTrackFormDto dto) {
		SpfTradeTrackForm spfTradeTrackForm = spfTradeTrackFormDao.get(dto.getId());
		if (spfTradeTrackForm == null) {
			return -1;
		}
		SpfTradeContract spfTradeContract = spfTradeContractDao.get(spfTradeTrackForm.getTradeContractId());
		if (StrUtil.equals(spfTradeContract.getTradeContractStatus(), "5") ||
				StrUtil.equals(spfTradeContract.getTradeContractStatus(), "6")) {
			return -2;
		}
		BeanUtil.copyBeans(dto, spfTradeTrackForm);
		spfTradeTrackForm.setModifyBy(dto.getModifyBy());
		spfTradeTrackForm.setModifyDate(dto.getModifyDate());
		int count =spfTradeTrackFormDao.save(spfTradeTrackForm);
		return count;
	}
	
	public PageResultDto<SpfTradeTrackFormDto> getList(SpfTradeContractDto dto) {
		
		return spfTradeTrackFormDao.getList(dto);
	}
}
